![]() |
PATH![]() |
A mouse event occurs when you click somewhere in the visible screen area. A function that handles mouse events must check the location of the mouse click and take action as appropriate. If the mouse event took place in a window that corresponds to a frame, you must pass the event to the frame so the Java applet can take proper action.
Listing 1-16 shows a function, handleMouse , that handles a mouse event.
Listing 1-16 Handling a mouse event
void handleMouse(const EventRecord* eve)
{
WindowPtr win;
short part = FindWindow(eve->where, &win);
switch (part) {
case inMenuBar: {
long mResult = MenuSelect(eve->where);
if (mResult != 0)
menuHit(mResult >> 16, mResult & 0xffff);
} break;
case inDesk:
break;
case inSysWindow:
SystemClick(eve, win);
break;
case inContent:
if (win != FrontWindow())
SelectWindow(win);
else {
JMFrameRef frame = (JMFrameRef) GetWRefCon(win);
if (frame) {
/* convert the mouse position to window local */
/* coordinates and pass it into the Java */
/* environment */
Point localPos = eve->where;
SetPort(win);
GlobalToLocal(&localPos);
JMFrameClick(frame, localPos, eve->modifiers);
}
}
break;
case inDrag: {
Rect r = (**GetGrayRgn()).rgnBBox;
DragWindow(win, eve->where, &r);
} break;
case inGoAway: {
/* request that the frame go away--it will call to the */
/* frame through a callback if it actually does */
JMFrameRef frame = (JMFrameRef) GetWRefCon(win);
if (frame)
JMFrameGoAway(frame);
} break;
case inGrow: {
union GrowResults {
Point asPt;
long asLong;
} results;
JMFrameRef frame = (JMFrameRef) GetWRefCon(win);
Rect rGrow = { 30, 30, 5000, 5000 };
results.asLong = GrowWindow(win, eve->where, &rGrow);
if (frame != nil && results.asLong != 0) {
/* request that the frame resize itself--it will call */
/* to the frame through a callback if it actually does */
Rect r;
r.left = 0;
r.top = 0;
r.right = results.asPt.h;
r.bottom = results.asPt.v;
r.bottom -= 15;
JMSetFrameSize(frame, &r);
}
} break;
default:
break;
}
}
This example uses the Mac OS Toolbox function FindWindow to determine the location of the mouse click and then takes action depending on the location.
The inMenuBar , inDesk , and inSysWindow cases are handled as in any Mac OS application.
If the mouse click occurs in a window's content area (
inContent
),
handleMouse
checks to see if the window is active. If not, the window is selected (it receives an activate event and possibly a resume event). If the window is currently active, the local position of the mouse within the window is calculated and the coordinates sent to the corresponding frame using the
JMFrameClick
function
JMFrameClick
. (If you need to send modifier information, you should call the
JMFrameClickWithEventRecord
function
JMFrameClickWithEventRecord
instead.) In a similar manner, you can also send a mouse-over event by calling the
JMFrameMouseOver
function
JMFrameMouseOver
before the
JMIdle
call in the main event loop.
If the mouse click is in the drag region ( inDrag ), the Mac OS Toolbox function DragWindow is called to move the window. You do not have to pass any information to the corresponding frame, since the Java runtime environment does not worry about the relative position of frames.
If the mouse click is on the close box (
inGoAway
), the code notifies the corresponding frame using the
JMFrameGoAway
function
JMFrameGoAway
. Any user-visible response to this action (such as removing the window) is handled by the AWT using the callbacks you specified when instantiating the AWT context.
If the mouse click is in a window's size box (
inGrow
), the code calls the Mac OS Toolbox function
GrowWindow
to track the new size of the window. The new dimensions are passed to the frame using the
JMSetFrameSize
function
JMSetFrameSize
. The dimensions of the window are updated using an AWT context callback.
Note
If the new window dimensions are too large or too small (because of screen constraints or some arbitrary limit), the window should be adjusted to a preferred size.